﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace Handrise.CDCable.Mobile.Business
{
    public class DeviceService
    {

        /// <summary>
        /// 通过巡视设备获取该设备上的巡视详情
        /// </summary>
        /// <param name="planDeviceID"></param>
        /// <returns></returns>
        public List<PatrolDetail> GetPatrolExceptionsByDevice(string planDeviceID)
        {
            return new List<PatrolDetail>();
        }

        /// <summary>
        /// 添加一个巡视详情
        /// </summary>
        /// <param name="patrolException"></param>
        /// <returns></returns>
        public bool AddPatrolException(PatrolDetail patrolException)
        {
            return true;
        }

        /// <summary>
        /// 删除一个巡视异常，取消异常
        /// </summary>
        /// <param name="patrolExceptionID"></param>
        /// <returns></returns>
        public bool DeletePatrolException(string patrolExceptionID)
        {
            return true;
        }

        /// <summary>
        /// 获取设备
        /// </summary>
        /// <param name="deviceID"></param>
        /// <returns></returns>
        public Device GetDevice(string deviceID)
        {
            return AppLogic.dataService.GetEntityByKey<Device>(deviceID);
        }

        /// <summary>
        /// 通过设备编码查找设备
        /// </summary>
        /// <param name="deviceCode"></param>
        /// <returns></returns>
        public Device GetDeviceByCode(string deviceCode)
        {
            return AppLogic.dataService.GetFirstEntity<Device>("Code ='" + deviceCode + "'","") ;
        }

        /// <summary>
        /// 更新多个设备信息，先插入，插入失败再更新
        /// </summary>
        /// <param name="devices">新的设备数据</param>
        /// <param name="deviceDetails">新的设备详细数据</param>
        /// <param name="deviceType">设备类型</param>
        /// <returns></returns>
        public bool UpdateDevices(List<Device> devices,List<object> deviceDetails,string deviceType)
        {
            try
            {
                //更新设备基本信息
                foreach (Device device in devices)
                {
                    if (AppLogic.dataService.Add<Device>(device) != 1)
                        AppLogic.dataService.Update<Device>(device);
                }

                //更新设备的详情信息
                foreach (object deviceDetail in deviceDetails)
                {
                    switch (deviceType)
                    {
                        case "Emamong":
                            if (AppLogic.dataService.Add<EM_Among>((EM_Among)deviceDetail) != 1)
                                AppLogic.dataService.Update<EM_Among>((EM_Among)deviceDetail);
                            break;
                        case "Emarre":
                            if (AppLogic.dataService.Add<EM_Arre>((EM_Arre)deviceDetail) != 1)
                                AppLogic.dataService.Update<EM_Arre>((EM_Arre)deviceDetail);
                            break;
                        case "com.techstar.pda.entity.Emcables":
                            if (AppLogic.dataService.Add<EM_Cable>((EM_Cable)deviceDetail) != 1)
                                AppLogic.dataService.Update<EM_Cable>((EM_Cable)deviceDetail);
                            break;
                        case "Emcameras":
                            if (AppLogic.dataService.Add<EM_Camera>((EM_Camera)deviceDetail) != 1)
                                AppLogic.dataService.Update<EM_Camera>((EM_Camera)deviceDetail);
                            break;
                        case "Emcameramain":
                            if (AppLogic.dataService.Add<EM_CameraMain>((EM_CameraMain)deviceDetail) != 1)
                                AppLogic.dataService.Update<EM_CameraMain>((EM_CameraMain)deviceDetail);
                            break;
                        case "Emculvert":
                            if (AppLogic.dataService.Add<EM_Culvert>((EM_Culvert)deviceDetail) != 1)
                                AppLogic.dataService.Update<EM_Culvert>((EM_Culvert)deviceDetail);
                            break;
                        case "Emelectronicposition":
                            if (AppLogic.dataService.Add<EM_ElectronicPosition>((EM_ElectronicPosition)deviceDetail) != 1)
                                AppLogic.dataService.Update<EM_ElectronicPosition>((EM_ElectronicPosition)deviceDetail);
                            break;
                        case "Emerou":
                            if (AppLogic.dataService.Add<EM_Erou>((EM_Erou)deviceDetail) != 1)
                                AppLogic.dataService.Update<EM_Erou>((EM_Erou)deviceDetail);
                            break;
                        case "Emfans":
                            if (AppLogic.dataService.Add<EM_Fan>((EM_Fan)deviceDetail) != 1)
                                AppLogic.dataService.Update<EM_Fan>((EM_Fan)deviceDetail);
                            break;
                        case "Emfiredoors":
                            if (AppLogic.dataService.Add<EM_FireDoor>((EM_FireDoor)deviceDetail) != 1)
                                AppLogic.dataService.Update<EM_FireDoor>((EM_FireDoor)deviceDetail);
                            break;
                        case "Emgateaccess":
                            if (AppLogic.dataService.Add<EM_GateAccess>((EM_GateAccess)deviceDetail) != 1)
                                AppLogic.dataService.Update<EM_GateAccess>((EM_GateAccess)deviceDetail);
                            break;
                        case "Emjoin":
                            if (AppLogic.dataService.Add<EM_Join>((EM_Join)deviceDetail) != 1)
                                AppLogic.dataService.Update<EM_Join>((EM_Join)deviceDetail);
                            break;
                        case "Emline":
                            if (AppLogic.dataService.Add<EM_Line>((EM_Line)deviceDetail) != 1)
                                AppLogic.dataService.Update<EM_Line>((EM_Line)deviceDetail);
                            break;
                        case "Emmoin":
                            if (AppLogic.dataService.Add<EM_Moin>((EM_Moin)deviceDetail) != 1)
                                AppLogic.dataService.Update<EM_Moin>((EM_Moin)deviceDetail);
                            break;
                        case "Emmonitorgas":
                            if (AppLogic.dataService.Add<EM_MonitorGas>((EM_MonitorGas)deviceDetail) != 1)
                                AppLogic.dataService.Update<EM_MonitorGas>((EM_MonitorGas)deviceDetail);
                            break;
                        case "Emmonitorgi":
                            if (AppLogic.dataService.Add<EM_MonitoRgi>((EM_MonitoRgi)deviceDetail) != 1)
                                AppLogic.dataService.Update<EM_MonitoRgi>((EM_MonitoRgi)deviceDetail);
                            break;
                        case "Emmonitorwater":
                            if (AppLogic.dataService.Add<EM_MonitorWater>((EM_MonitorWater)deviceDetail) != 1)
                                AppLogic.dataService.Update<EM_MonitorWater>((EM_MonitorWater)deviceDetail);
                            break;
                        case "Emnet":
                            if (AppLogic.dataService.Add<EM_Net>((EM_Net)deviceDetail) != 1)
                                AppLogic.dataService.Update<EM_Net>((EM_Net)deviceDetail);
                            break;
                        case "Empda":
                            if (AppLogic.dataService.Add<EM_PDA>((EM_PDA)deviceDetail) != 1)
                                AppLogic.dataService.Update<EM_PDA>((EM_PDA)deviceDetail);
                            break;
                        case "com.techstar.pda.entity.Empump":
                            if (AppLogic.dataService.Add<EM_Pump>((EM_Pump)deviceDetail) != 1)
                                AppLogic.dataService.Update<EM_Pump>((EM_Pump)deviceDetail);
                            break;
                        case "Emrtu":
                            if (AppLogic.dataService.Add<EM_Rtu>((EM_Rtu)deviceDetail) != 1)
                                AppLogic.dataService.Update<EM_Rtu>((EM_Rtu)deviceDetail);
                            break;
                        case "Emrtuchannel":
                            if (AppLogic.dataService.Add<EM_RtuChannel>((EM_RtuChannel)deviceDetail) != 1)
                                AppLogic.dataService.Update<EM_RtuChannel>((EM_RtuChannel)deviceDetail);
                            break;
                        case "Emservice":
                            if (AppLogic.dataService.Add<EM_Service>((EM_Service)deviceDetail) != 1)
                                AppLogic.dataService.Update<EM_Service>((EM_Service)deviceDetail);
                            break;
                        case "Emsubstation":
                            if (AppLogic.dataService.Add<EM_Substation>((EM_Substation)deviceDetail) != 1)
                                AppLogic.dataService.Update<EM_Substation>((EM_Substation)deviceDetail);
                            break;
                        case "Emswit":
                            if (AppLogic.dataService.Add<EM_Swit>((EM_Swit)deviceDetail) != 1)
                                AppLogic.dataService.Update<EM_Swit>((EM_Swit)deviceDetail);
                            break;
                        case "Emtemfiber":
                            if (AppLogic.dataService.Add<EM_Temfiber>((EM_Temfiber)deviceDetail) != 1)
                                AppLogic.dataService.Update<EM_Temfiber>((EM_Temfiber)deviceDetail);
                            break;
                        case "Emtempmain":
                            if (AppLogic.dataService.Add<EM_Tempmain>((EM_Tempmain)deviceDetail) != 1)
                                AppLogic.dataService.Update<EM_Tempmain>((EM_Tempmain)deviceDetail);
                            break;
                        case "Emterm":
                            if (AppLogic.dataService.Add<EM_Term>((EM_Term)deviceDetail) != 1)
                                AppLogic.dataService.Update<EM_Term>((EM_Term)deviceDetail);
                            break;
                        case "Emterminal":
                            if (AppLogic.dataService.Add<EM_Terminal>((EM_Terminal)deviceDetail) != 1)
                                AppLogic.dataService.Update<EM_Terminal>((EM_Terminal)deviceDetail);
                            break;
                        case "Emtrench":
                            if (AppLogic.dataService.Add<EM_Trench>((EM_Trench)deviceDetail) != 1)
                                AppLogic.dataService.Update<EM_Trench>((EM_Trench)deviceDetail);
                            break;
                        case "Emtube":
                            if (AppLogic.dataService.Add<EM_Tube>((EM_Tube)deviceDetail) != 1)
                                AppLogic.dataService.Update<EM_Tube>((EM_Tube)deviceDetail);
                            break;
                        case "Emtunnel":
                            if (AppLogic.dataService.Add<EM_Tunnel>((EM_Tunnel)deviceDetail) != 1)
                                AppLogic.dataService.Update<EM_Tunnel>((EM_Tunnel)deviceDetail);
                            break;
                        case "Emvent":
                            if (AppLogic.dataService.Add<EM_Vent>((EM_Vent)deviceDetail) != 1)
                                AppLogic.dataService.Update<EM_Vent>((EM_Vent)deviceDetail);
                            break;
                        case "Emwell":
                            if (AppLogic.dataService.Add<EM_Well>((EM_Well)deviceDetail) != 1)
                                AppLogic.dataService.Update<EM_Well>((EM_Well)deviceDetail);
                            break;
                        default:
                            break;
                    }
                }
                return true;
            }
            catch (Exception e)
            {
                return false;
            }

        }

        /// <summary>
        /// 获取设备长度（电力隧道和电缆线路）
        /// </summary>
        /// <param name="deviceID"></param>
        /// <param name="deviceType"></param>
        /// <returns></returns>
        public string GetDeviceLength(string deviceID, string deviceType)
        {
            switch (deviceType)
            {
                case "电力隧道":
                    EM_Tunnel em_Tunnel = AppLogic.dataService.GetFirstEntity<EM_Tunnel>("DeviceID = '" + deviceID + "'", "");
                    if (em_Tunnel != null)
                        return em_Tunnel.LENGTH;

                    break;
                case "电缆线路":
                    EM_Line em_Line = AppLogic.dataService.GetFirstEntity<EM_Line>("DeviceID = '" + deviceID + "'", "");
                    if (em_Line != null)
                        return em_Line.LENGTH;
                    break;
                default:
                    break;
            }

            return "";

        }

        /// <summary>
        /// 获取设备关联的线路或隧道信息
        /// </summary>
        /// <param name="deviceID">设备ID</param>
        /// <param name="parentDeviceLength">线路或隧道的长度</param>
        /// <returns>父设备实体</returns>
        public bool GetLineOrTunnelInfo(string deviceID,out string length,out string lineOrTunnelName,out string type)
        {
            length = "";
            lineOrTunnelName = "";
            type = "";

            Device device = GetDevice(deviceID);

            if (device == null)
                return false;

            if (device.DeviceType == "水位测量仪" || device.DeviceType == "水泵" || device.DeviceType == "气体测量仪" || device.DeviceType == "门禁" || device.DeviceType == "防火门" || device.DeviceType == "摄像头")
            {
                string deviceCode = "";
                if (device.DeviceType == "水位测量仪")
                {
                    //获取关联的隧道Code
                    EM_MonitorWater em_MonitorWater = AppLogic.dataService.GetFirstEntity<EM_MonitorWater>("DeviceID = '" + device.DeviceID + "'", "");
                    if (em_MonitorWater == null)
                        return false ;

                    deviceCode = em_MonitorWater.TUNNELSEQUENCE;
                }

                if (device.DeviceType == "水泵")
                {
                    //获取关联的隧道Code
                    EM_Pump em_Pump = AppLogic.dataService.GetFirstEntity<EM_Pump>("DeviceID = '" + device.DeviceID + "'", "");
                    if (em_Pump == null)
                        return false ;
                    deviceCode = em_Pump.TUNNEL_FDEVSEQUENCE;
                }

                if (device.DeviceType == "气体测量仪")
                {
                    //获取关联的隧道Code
                    EM_MonitorGas em_MonitorGas = AppLogic.dataService.GetFirstEntity<EM_MonitorGas>("DeviceID = '" + device.DeviceID + "'", "");
                    if (em_MonitorGas == null)
                        return false;
                    deviceCode = em_MonitorGas.TUNNELSEQUENCE;
                }

                if (device.DeviceType == "门禁")
                {
                    //获取关联的隧道Code
                    EM_GateAccess em_GateAccess = AppLogic.dataService.GetFirstEntity<EM_GateAccess>("DeviceID = '" + device.DeviceID + "'", "");
                    if (em_GateAccess == null)
                        return false ;
                    deviceCode = em_GateAccess.TUNNEL_FDEVSEQUENCE;
                }

                if (device.DeviceType == "防火门")
                {
                    //获取关联的隧道Code
                    EM_FireDoor em_FireDoor = AppLogic.dataService.GetFirstEntity<EM_FireDoor>("DeviceID = '" + device.DeviceID + "'", "");
                    if (em_FireDoor == null)
                        return false ;
                    deviceCode = em_FireDoor.TUNNEL_FDEVSEQUENCE;
                }

                if (device.DeviceType == "摄像头")
                {
                    //获取关联的隧道Code
                    EM_Camera em_Camera = AppLogic.dataService.GetFirstEntity<EM_Camera>("DeviceID = '" + device.DeviceID + "'", "");
                    if (em_Camera == null)
                        return false ;
                    deviceCode = em_Camera.TUNNELSEQUENCE;
                }

                //通过Code获取设备ID
                Device em_TunnelDevice = AppLogic.dataService.GetFirstEntity<Device>("Code = '" + deviceCode + "'", "");
                if (em_TunnelDevice == null)
                    return false ;

                EM_Tunnel em_Tunnel = AppLogic.dataService.GetFirstEntity<EM_Tunnel>("DeviceID = '" + em_TunnelDevice.DeviceID + "'", "");
                if (em_Tunnel == null)
                    return false ;

                length = em_Tunnel.LENGTH;
                lineOrTunnelName = em_TunnelDevice.Name;
                type = em_TunnelDevice.DeviceType;
                return true;
            }
            else if (device.DeviceType == "接地电流测量仪")
            {
                string deviceCode = "";

                //获取关联的线路Code
                EM_MonitoRgi em_MonitoRgi = AppLogic.dataService.GetFirstEntity<EM_MonitoRgi>("DeviceID = '" + device.DeviceID + "'", "");
                if (em_MonitoRgi == null)
                    return false ;
                deviceCode = em_MonitoRgi.LINESEQUENCE;

                //通过Code获取设备ID
                Device em_LineDevice = AppLogic.dataService.GetFirstEntity<Device>("Code = '" + deviceCode + "'", "");
                if (em_LineDevice == null)
                    return false ;

                EM_Line em_Line = AppLogic.dataService.GetFirstEntity<EM_Line>("DeviceID = '" + em_LineDevice.DeviceID + "'", "");
                if (em_Line == null)
                    return false;

                length = em_Line.LENGTH;
                lineOrTunnelName = em_LineDevice.Name;
                type = em_LineDevice.DeviceType;
                return true;
            }
            else
            {
                Device parentDevice = GetDevice(device.ParentDeviceID);

                if (parentDevice == null)
                    return false;

                switch (parentDevice.DeviceType)
                {
                    case "电力隧道":
                        EM_Tunnel em_Tunnel = AppLogic.dataService.GetFirstEntity<EM_Tunnel>("DeviceID = '" + parentDevice.DeviceID + "'", "");
                        if (em_Tunnel != null)
                        {
                            length = em_Tunnel.LENGTH;
                            lineOrTunnelName = parentDevice.Name;
                            type = parentDevice.DeviceType;
                        }
                        break;
                    case "电缆线路":
                        EM_Line em_Line = AppLogic.dataService.GetFirstEntity<EM_Line>("DeviceID = '" + parentDevice.DeviceID + "'", "");
                        if (em_Line != null)
                        {
                            length = em_Line.LENGTH;
                            lineOrTunnelName = parentDevice.Name;
                            type = parentDevice.DeviceType;
                        }
                        break;
                    default:
                        break;
                }
                
            }

            return true ;
        }

        /// <summary>
        /// 获取符合条件的设备
        /// </summary>
        /// <param name="deviceCode"></param>
        /// <param name="deviceName"></param>
        /// <param name="deviceType"></param>
        /// <returns></returns>
        public List<Device> GetDevices(string deviceCode, string deviceName, string deviceType)
        {
            string filter = "";
            if (deviceCode != "")
                filter += "and (Code = '" + deviceCode + "') ";
            if (deviceName != "")
                filter += "and (Name like '%" + deviceName + "%') ";
            if (deviceType != "")
                filter += "and (DeviceType = '" + deviceType + "') ";

            if (filter != "")
                filter = filter.Remove(0, 4);

            return AppLogic.dataService.Find<Device>(filter, "");

        }

        /// <summary>
        /// 获取设备详情
        /// </summary>
        /// <typeparam name="T">设备详情类</typeparam>
        /// <param name="deviceID"></param>
        /// <returns></returns>
        public object GetDeviceDetail(string deviceID)
        {
            Device device = AppLogic.dataService.GetEntityByKey<Device>(deviceID);

            if(device == null)
                return null;

            switch (device.DeviceType)
            {
                case "电缆小间":
                    EM_AmongDetail em_AmongDetail = new EM_AmongDetail();
                    em_AmongDetail.Device = device;
                    em_AmongDetail.EM_Among = AppLogic.dataService.GetFirstEntity<EM_Among>("DeviceID = '" + device.DeviceID + "'", "");
                    return em_AmongDetail;
                case "避雷器":
                    EM_ArreDetail em_ArreDetail = new EM_ArreDetail();
                    em_ArreDetail.Device = device;
                    em_ArreDetail.EM_Arre = AppLogic.dataService.GetFirstEntity<EM_Arre>("DeviceID = '" + device.DeviceID + "'", "");
                    return em_ArreDetail;
                case "电缆段":
                    EM_CableDetail em_CableDetail = new EM_CableDetail();
                    em_CableDetail.Device = device;
                    em_CableDetail.EM_Cable = AppLogic.dataService.GetFirstEntity<EM_Cable>("DeviceID = '" + device.DeviceID + "'", "");
                    return em_CableDetail;
                case "摄像头":
                    EM_CameraDetail em_CameraDetail = new EM_CameraDetail();
                    em_CameraDetail.Device = device;
                    em_CameraDetail.EM_Camera = AppLogic.dataService.GetFirstEntity<EM_Camera>("DeviceID = '" + device.DeviceID + "'", "");
                    return em_CameraDetail;
                case "摄像监控主机":
                    EM_CameraMainDetail em_CameraMainDetail = new EM_CameraMainDetail();
                    em_CameraMainDetail.Device = device;
                    em_CameraMainDetail.EM_CameraMain = AppLogic.dataService.GetFirstEntity<EM_CameraMain>("DeviceID = '" + device.DeviceID + "'", "");
                    return em_CameraMainDetail;
                case "涵洞":
                    EM_CulvertDetail em_CulvertDetail = new EM_CulvertDetail();
                    em_CulvertDetail.Device = device;
                    em_CulvertDetail.EM_Culvert = AppLogic.dataService.GetFirstEntity<EM_Culvert>("DeviceID = '" + device.DeviceID + "'", "");
                    return em_CulvertDetail;
                case "电子定位球":
                    EM_ElectronicPositionDetail em_ElectronicPositionDetail = new EM_ElectronicPositionDetail();
                    em_ElectronicPositionDetail.Device = device;
                    em_ElectronicPositionDetail.EM_ElectronicPosition = AppLogic.dataService.GetFirstEntity<EM_ElectronicPosition>("DeviceID = '" + device.DeviceID + "'", "");
                    return em_ElectronicPositionDetail;
                case "互联箱/接地箱":
                    EM_ErouDetail em_ErouDetail = new EM_ErouDetail();
                    em_ErouDetail.Device = device;
                    em_ErouDetail.EM_Erou = AppLogic.dataService.GetFirstEntity<EM_Erou>("DeviceID = '" + device.DeviceID + "'", "");
                    return em_ErouDetail;
                case "风机":
                    EM_FanDetail em_FanDetail = new EM_FanDetail();
                    em_FanDetail.Device = device;
                    em_FanDetail.EM_Fan = AppLogic.dataService.GetFirstEntity<EM_Fan>("DeviceID = '" + device.DeviceID + "'", "");
                    return em_FanDetail;
                case "防火门":
                    EM_FireDoorDetail em_FireDoorDetail = new EM_FireDoorDetail();
                    em_FireDoorDetail.Device = device;
                    em_FireDoorDetail.EM_FireDoor = AppLogic.dataService.GetFirstEntity<EM_FireDoor>("DeviceID = '" + device.DeviceID + "'", "");
                    return em_FireDoorDetail;
                case "门禁":
                    EM_GateAccessDetail em_GateAccessDetail = new EM_GateAccessDetail();
                    em_GateAccessDetail.Device = device;
                    em_GateAccessDetail.EM_GateAccess = AppLogic.dataService.GetFirstEntity<EM_GateAccess>("DeviceID = '" + device.DeviceID + "'", "");
                    return em_GateAccessDetail;
                case "电缆接头":
                    EM_JoinDetail em_JoinDetail = new EM_JoinDetail();
                    em_JoinDetail.Device = device;
                    em_JoinDetail.EM_Join = AppLogic.dataService.GetFirstEntity<EM_Join>("DeviceID = '" + device.DeviceID + "'", "");
                    return em_JoinDetail;
                case "电缆线路":
                    EM_LineDetail em_LineDetail = new EM_LineDetail();
                    em_LineDetail.Device = device;
                    em_LineDetail.EM_Line = AppLogic.dataService.GetFirstEntity<EM_Line>("DeviceID = '" + device.DeviceID + "'", "");
                    return em_LineDetail;
                case "检查井监测仪":
                    EM_MoinDetail em_MoinDetail = new EM_MoinDetail();
                    em_MoinDetail.Device = device;
                    em_MoinDetail.EM_Moin = AppLogic.dataService.GetFirstEntity<EM_Moin>("DeviceID = '" + device.DeviceID + "'", "");
                    return em_MoinDetail;
                case "气体测量仪":
                    EM_MonitorGasDetail em_MonitorGasDetail = new EM_MonitorGasDetail();
                    em_MonitorGasDetail.Device = device;
                    em_MonitorGasDetail.EM_MonitorGas = AppLogic.dataService.GetFirstEntity<EM_MonitorGas>("DeviceID = '" + device.DeviceID + "'", "");
                    return em_MonitorGasDetail;
                case "接地电流测量仪":
                    EM_MonitoRgiDetail em_MonitoRgiDetail = new EM_MonitoRgiDetail();
                    em_MonitoRgiDetail.Device = device;
                    em_MonitoRgiDetail.EM_MonitoRgi = AppLogic.dataService.GetFirstEntity<EM_MonitoRgi>("DeviceID = '" + device.DeviceID + "'", "");
                    return em_MonitoRgiDetail;
                case "水位测量仪":
                    EM_MonitorWaterDetail em_MonitorWaterDetail = new EM_MonitorWaterDetail();
                    em_MonitorWaterDetail.Device = device;
                    em_MonitorWaterDetail.EM_MonitorWater = AppLogic.dataService.GetFirstEntity<EM_MonitorWater>("DeviceID = '" + device.DeviceID + "'", "");
                    return em_MonitorWaterDetail;
                case "网络设备":
                    EM_NetDetail em_NetDetail = new EM_NetDetail();
                    em_NetDetail.Device = device;
                    em_NetDetail.EM_Net = AppLogic.dataService.GetFirstEntity<EM_Net>("DeviceID = '" + device.DeviceID + "'", "");
                    return em_NetDetail;
                case "PDA":
                    EM_PDADetail em_PDADetail = new EM_PDADetail();
                    em_PDADetail.Device = device;
                    em_PDADetail.EM_PDA = AppLogic.dataService.GetFirstEntity<EM_PDA>("DeviceID = '" + device.DeviceID + "'", "");
                    return em_PDADetail;
                case "水泵":
                    EM_PumpDetail em_PumpDetail = new EM_PumpDetail();
                    em_PumpDetail.Device = device;
                    em_PumpDetail.EM_Pump = AppLogic.dataService.GetFirstEntity<EM_Pump>("DeviceID = '" + device.DeviceID + "'", "");
                    return em_PumpDetail;
                case "监控RTU":
                    EM_RtuDetail em_RtuDetail = new EM_RtuDetail();
                    em_RtuDetail.Device = device;
                    em_RtuDetail.EM_Rtu = AppLogic.dataService.GetFirstEntity<EM_Rtu>("DeviceID = '" + device.DeviceID + "'", "");
                    return em_RtuDetail;
                case "rtu通道表":
                    EM_RtuChannelDetail em_RtuChannelDetail = new EM_RtuChannelDetail();
                    em_RtuChannelDetail.Device = device;
                    em_RtuChannelDetail.EM_RtuChannel = AppLogic.dataService.GetFirstEntity<EM_RtuChannel>("DeviceID = '" + device.DeviceID + "'", "");
                    return em_RtuChannelDetail;
                case "服务器":
                    EM_ServiceDetail em_ServiceDetail = new EM_ServiceDetail();
                    em_ServiceDetail.Device = device;
                    em_ServiceDetail.EM_Service = AppLogic.dataService.GetFirstEntity<EM_Service>("DeviceID = '" + device.DeviceID + "'", "");
                    return em_ServiceDetail; 
                case "变电站":
                    EM_SubstationDetail em_SubstationDetail = new EM_SubstationDetail();
                    em_SubstationDetail.Device = device;
                    em_SubstationDetail.EM_Substation = AppLogic.dataService.GetFirstEntity<EM_Substation>("DeviceID = '" + device.DeviceID + "'", "");
                    return em_SubstationDetail;
                case "电缆刀闸":
                    EM_SwitDetail em_SwitDetail = new EM_SwitDetail();
                    em_SwitDetail.Device = device;
                    em_SwitDetail.EM_Swit = AppLogic.dataService.GetFirstEntity<EM_Swit>("DeviceID = '" + device.DeviceID + "'", "");
                    return em_SwitDetail;
                case "测温光纤":
                    EM_TemfiberDetail em_TemfiberDetail = new EM_TemfiberDetail();
                    em_TemfiberDetail.Device = device;
                    em_TemfiberDetail.EM_Temfiber = AppLogic.dataService.GetFirstEntity<EM_Temfiber>("DeviceID = '" + device.DeviceID + "'", "");
                    return em_TemfiberDetail;
                case "光纤测温主机":
                    EM_TempmainDetail em_TempmainDetail = new EM_TempmainDetail();
                    em_TempmainDetail.Device = device;
                    em_TempmainDetail.EM_Tempmain = AppLogic.dataService.GetFirstEntity<EM_Tempmain>("DeviceID = '" + device.DeviceID + "'", "");
                    return em_TempmainDetail;
                case "电缆终端":
                    EM_TermDetail em_TermDetail = new EM_TermDetail();
                    em_TermDetail.Device = device;
                    em_TermDetail.EM_Term = AppLogic.dataService.GetFirstEntity<EM_Term>("DeviceID = '" + device.DeviceID + "'", "");
                    return em_TermDetail;
                case "终端塔":
                    EM_TerminalDetail em_TerminalDetail = new EM_TerminalDetail();
                    em_TerminalDetail.Device = device;
                    em_TerminalDetail.EM_Terminal = AppLogic.dataService.GetFirstEntity<EM_Terminal>("DeviceID = '" + device.DeviceID + "'", "");
                    return em_TerminalDetail;
                case "浅沟排管":
                    EM_TrenchDetail em_TrenchDetail = new EM_TrenchDetail();
                    em_TrenchDetail.Device = device;
                    em_TrenchDetail.EM_Trench = AppLogic.dataService.GetFirstEntity<EM_Trench>("DeviceID = '" + device.DeviceID + "'", "");
                    return em_TrenchDetail;
                case "电缆排管":
                    EM_TubeDetail em_TubeDetail = new EM_TubeDetail();
                    em_TubeDetail.Device = device;
                    em_TubeDetail.EM_Tube = AppLogic.dataService.GetFirstEntity<EM_Tube>("DeviceID = '" + device.DeviceID + "'", "");
                    return em_TubeDetail;
                case "电力隧道":
                    EM_TunnelDetail em_TunnelDetail = new EM_TunnelDetail();
                    em_TunnelDetail.Device = device;
                    em_TunnelDetail.EM_Tunnel = AppLogic.dataService.GetFirstEntity<EM_Tunnel>("DeviceID = '" + device.DeviceID + "'", "");
                    return em_TunnelDetail;
                case "通风亭":
                    EM_VentDetail em_VentDetail = new EM_VentDetail();
                    em_VentDetail.Device = device;
                    em_VentDetail.EM_Vent = AppLogic.dataService.GetFirstEntity<EM_Vent>("DeviceID = '" + device.DeviceID + "'", "");
                    return em_VentDetail;
                case "检查井":
                    EM_WellDetail em_WellDetail = new EM_WellDetail();
                    em_WellDetail.Device = device;
                    em_WellDetail.EM_Well = AppLogic.dataService.GetFirstEntity<EM_Well>("DeviceID = '" + device.DeviceID + "'", "");
                    return em_WellDetail;
                default:
                    return null;
            }

        }

    }
}
